package com.yzy.soft.system.controller.page;


import com.alibaba.fastjson.JSONObject;
import com.yzy.soft.common.persistence.model.HtmlTable;
import com.yzy.soft.common.persistence.model.HtmlTableCol;
import com.yzy.soft.core.base.controller.BaseController;
import com.yzy.soft.system.util.JSONUtil;
import com.yzy.soft.system.util.StringUtil;
import org.springframework.web.bind.annotation.*;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yinzhiyuan
 * @since 2018-01-03
 */
@RestController
@RequestMapping("/api/")
public class RunController extends BaseController{


    @PostMapping("runHtml/")
    public String runHtml(@RequestParam(name = "html" ,required = true) String html) {
        System.out.println("html:"+html);
        String layequalsign = StringUtil.regexReplace(html, "layequalsign", "=");
        return StringUtil.regexReplace(layequalsign, "layscrlayipttag", "script");
    }

    @PostMapping("runHtml/table")
    public String runHtmlTable(@RequestParam(name = "json" ,required = true) String json) {
        System.out.println("json:"+json);
        List<RunHtml> runHtmls = JSONObject.parseArray(json, RunHtml.class);

        List<HtmlTableCol> cols =new ArrayList<>();
        List<HtmlTableCol> htmlTableCols =new ArrayList<>();
        HtmlTableCol htmlTableCol =null;

        for (int i = 0; i <runHtmls.size() ; i++) {
            htmlTableCol =new HtmlTableCol();
            RunHtml runHtml = runHtmls.get(i);
            htmlTableCol.setField(runHtml.getField());
            htmlTableCol.setTitle(runHtml.getDisplay_name());
            htmlTableCol.setWidth(runHtml.getDisplay_length());
            htmlTableCols.add(htmlTableCol);
        }
        HtmlTable htmlTable =new HtmlTable();
        htmlTable.setElem("test-tableidElem");
        htmlTable.setIdTablein("test-tableid");
        htmlTable.setName("后台测试表格");
        htmlTable.setUrl("/metadatacolumn/by?tableId=154");
        htmlTable.setLimit(10);
        htmlTable.setCols(cols);
        String tablehtml=JSONObject.toJSONString(htmlTable);
        String tableColshtml=JSONObject.toJSONString(htmlTableCols);

        json = tablehtml.replaceAll("\"(\\w+)\"(\\s*:\\s*)", "$1$2");
        String htmltablecolsjson = tableColshtml.replaceAll("\"(\\w+)\"(\\s*:\\s*)", "$1$2");


        json=JSONUtil.JSONFormat_Layui_Table(json,htmltablecolsjson);
//        htmltablecolsjson=JSONUtil.JSONFormat_Layui_Table_Col(htmltablecolsjson);




//        ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
//        Invocable invocable = (Invocable) engine;
//        Object result = invocable.invokeFunction("eval", "'("+json.toString()+")'");
//        System.out.println(result) ;
//        System.out.println("tablehtml:"+json);
//        System.out.println("tablehtmlcols:"+htmltablecolsjson);










//        String result =htmltablecolsjson;


//        System.out.println("result:"+result);
//        String layequalsign = StringUtil.regexReplace(html, "layequalsign", "=");
        return json ;
    }

}

